#include "chooselanguage.h"
#include "ui_chooselanguage.h"

#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusReply>
#include <QtDBus/QDBusConnection>
#include <QDebug>

ChooseLanguage::ChooseLanguage(QWidget *parent) :
    Base(parent),
    ui(new Ui::ChooseLanguage)
{
    ui->setupUi(this);

    ui->zh_RBtn->setChecked(true);
    connect(ui->agree_Cbx, &QCheckBox::stateChanged, this, &ChooseLanguage::stateChanged);
}

ChooseLanguage::~ChooseLanguage()
{
    delete ui;
}

QList<QPair<QString, QString> > ChooseLanguage::getData()
{
    return QList<QPair<QString, QString> >() << QPair("language", m_lang);
}

bool ChooseLanguage::nextStep()
{
    QDBusInterface localedIface("org.freedesktop.locale1","/org/freedesktop/locale1", "org.freedesktop.locale1",
                                                           QDBusConnection::systemBus());
    QStringList lang;
    if (ui->zh_RBtn->isChecked()) {
        lang << "LANG=zh_CN.UTF-8" << "LANGUAGE=zh_CN";
        m_lang = "LANG=zh_CN.UTF-8:LANGUAGE=zh_CN";
    } else {
        lang  << "LANG=en_GB.UTF-8" << "LANGUAGE=en_GB";
        m_lang = "LANG=en_GB.UTF-8:LANGUAGE=en_GB";
    }
    QDBusReply<bool> reply = localedIface.callWithArgumentList(QDBus::AutoDetect, QStringLiteral("SetLocale"), QList<QVariant>() << lang << false);
    qInfo() << " set locale " << lang << ", result: " << reply.value() << ", error: " << reply.error();

    return true;
}

void ChooseLanguage::onStateChanged(int state)
{
    if (state != ui->agree_Cbx->checkState()) {
        ui->agree_Cbx->setCheckState(Qt::CheckState(state));
    }
}

void ChooseLanguage::on_agreement_Btn_released()
{
    Q_EMIT showAgreementDialog(AGREEMENTTYPE, ui->agree_Cbx->checkState());
}


void ChooseLanguage::on_privacy_Btn_pressed()
{
    Q_EMIT showAgreementDialog(PRIVACYTYPE, ui->agree_Cbx->checkState());
}


void ChooseLanguage::on_zh_RBtn_released()
{
    //TODO translate zh_CN
    qInfo() << "------zh_CN";
}


void ChooseLanguage::on_en_RBtn_released()
{
    //TODO translate english
    qInfo() << "------english";
}

